<?php

namespace App\Http\Controllers\Common\Traits;
use Illuminate\Support\Facades\Auth;

trait UserAuthTrait
{
    /**
     * 获取API登录的用户认证信息
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function guard()
    {
        return Auth::guard('api');
    }

    /**
     * 获取用户模型
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function userModel()
    {
        return $this->guard()->user();
    }

    /**
     * 获取用户ID
     * @return int|null
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function userId()
    {
        return $this->guard()->id();
    }

    /**
     * 获取用户组
     * @return int|null
     * User: john_chu
     * Email: john1668@qq.com
     */
    public function group()
    {
        return optional($this->userModel())->group_id ? optional($this->userModel())->group_id : 0;
    }

}